<?php

namespace App\Http\Models;

class TemplateJsonModel
{
    const FILE_NAME = 'template.json';
    /**
     * 读取template.json
     */
    public static function read($token, $template_name)
    {
        $app_folder = get_app_dir($token);
        $file_path = $app_folder . '/' . self::FILE_NAME;
        if(!file_exists($file_path) && $template_name) {
            $file_path = get_template_dir($template_name);
        }
        if(file_exists($file_path)) {
            $realpath = realpath($file_path);
            //$data = file_get_contents($file_path);
            $data = file_get_contents($realpath);
            return json_decode($data, true);
        } 
        return [];
    }

    /**
     * 写入template.json
     */
    public static function write($token, $data)
    {
        $app_folder = get_app_dir($token);
        $file_path = $app_folder . '/' . self::FILE_NAME;
        file_put_contents($file_path, json_encode($data));
        return 1;
    }
}
